package com.hnyyjsxy.controller;

import com.hnyyjsxy.domain.User;
import com.hnyyjsxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @date 2021/03/16
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //添加 如果添加成功就直接往页面返回一个字符串
    @RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String save(User user) {
        //调用业务层代码完成添加操作
        userService.save(user);
        return "添加成功";
    }


    //查询
    @RequestMapping("/findAll")
    public ModelAndView findAll(ModelAndView modelAndView) {
        //查询
        List<User> userList = userService.findAll();
        //封装数据
        modelAndView.addObject("userList", userList);
        //准备页面跳转
        modelAndView.setViewName("userList");
        return modelAndView;
    }
}
